package com.ptswitch.material.pages.user;

import java.util.List;

import org.apache.tapestry5.EventConstants;
import org.apache.tapestry5.annotations.InjectPage;
import org.apache.tapestry5.annotations.OnEvent;
import org.apache.tapestry5.annotations.Property;
import org.apache.tapestry5.ioc.annotations.Inject;
import org.hxzon.tapestry5.model.TreeModel;
import org.hxzon.tapestry5.model.ValueLabel;
import org.slf4j.Logger;

import com.ptswitch.material.model.common.Uzer;
import com.ptswitch.material.model.security.Permission;
import com.ptswitch.material.model.security.Role;
import com.ptswitch.material.pages.base.PageBase;
import com.ptswitch.material.service.PermissionService;
import com.ptswitch.material.service.RoleService;
import com.ptswitch.material.service.UserService;
import com.ptswitch.material.util.HlogConstants;
import com.ptswitch.material.util.ValueLabelUtil;
import com.ptswitch.material.util.security.PermissionTreeUtil;
import com.ptswitch.material.util.security.PermissionUtil;
import com.ptswitch.material.util.security.RoleEditUtil;

public class EditRole extends PageBase {
    @Inject
    private Logger logger;
    @Inject
    private RoleService roleSer;
    @Inject
    private PermissionService permissionSer;
    @Inject
    private UserService userSer;
    @InjectPage
    private ListRole listRole;
    //
    @Property
    private Role role;
    @Property
    private List<Permission> rolePermissions;
    @Property
    private List<Permission> permissions;
    @Property
    private TreeModel<Permission> permissionTreeModel = PermissionTreeUtil.permissionTreeModel;
    @Property
    private List<Uzer> users;
    @Property
    private List<Uzer> roleUsers;
    @Property
    private ValueLabel<Permission> permissionLabel;
    @Property
    private ValueLabel<Uzer> userLabel;

    @OnEvent(value = EventConstants.ACTIVATE)
    Object init(long id) {
        checkPermitted(PermissionUtil.RoleEdit);
        //init for display or update
        role = roleSer.findById(id);
        rolePermissions = RoleEditUtil.getPermissions(role);
        permissions = permissionSer.findAll();
        permissionLabel = ValueLabelUtil.permissionLabel;
        users = userSer.findAll();
        roleUsers = RoleEditUtil.getUsers(role);
        userLabel = ValueLabelUtil.uzerLabel;
        return null;
    }

    @OnEvent(value = EventConstants.PASSIVATE)
    Object onPassivate() {
        return role.getId();
    }

    @OnEvent(value = EventConstants.SUCCESS, component = "update")
    Object update() {
        checkPermitted(PermissionUtil.RoleEdit);
        roleSer.save(role);
        roleSer.saveRolePermissions(RoleEditUtil.toNewRolePermissions(rolePermissions, //
                role.getPermissions(), role));
        roleSer.saveUserRoles(RoleEditUtil.toNewUserRoles(roleUsers, //
                RoleEditUtil.getUsers(role), role));
        hlog(HlogConstants.updateRole, role);
        return listRole;
    }

}
